package com.qidian.by.free.config;

import com.qidian.by.free.service.FreeReceiveService;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Author: CoolPro
 * @Description:
 * @Time: 2024-05-17 21:16
 **/
@Configuration
public class FreeTemplateMakingListener {
    @Value("${queue-name}")
    private String queueName;
    @Value("${consumer-count}")
    private int consumerCount;
    @Value("${consumer-max-count}")
    private int maxConsumerCount;

    @Resource
    private CachingConnectionFactory cachingConnectionFactory;
    @Resource
    private FreeReceiveService freeReceiveService;

    //    配置第一个Listener
    @Bean
    public SimpleMessageListenerContainer listenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cachingConnectionFactory);
        container.setQueueNames(queueName);
//        当前消费者数量
        container.setConcurrentConsumers(consumerCount);
        container.setMaxConcurrentConsumers(maxConsumerCount);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(freeReceiveService);
        return container;
    }
}
